#include "StringUtilities.h"
#include <sstream>
#include <assert.h>

std::string ToString(float f)
{
	std::ostringstream oss;
	oss << f;
	return oss.str();
}

std::wstring ToWString(float f)
{
	std::ostringstream oss;
	oss << f;
	std::string s = oss.str();
	return std::wstring(s.begin(), s.end());
}

std::string ToString(int i)
{
	std::ostringstream oss;
	oss << i;
	return oss.str();
}

std::wstring ToWString(int i)
{
	std::ostringstream oss;
	oss << i;
	std::string s = oss.str();
	return std::wstring(s.begin(), s.end());
}

int ToInt(const std::string& str)
{
	return (int)atoi(str.c_str());
}

int ToInt(const std::wstring& s)
{
	std::string str(s.begin(), s.end());
	return (int)atoi(str.c_str());
}

float ToFloat(const std::string& str)
{
	return (float)atof(str.c_str());
}

float ToFloat(const std::wstring& s)
{
	std::string str(s.begin(), s.end());
	return (float)atof(str.c_str());
}

std::wstring StringToWString(const std::string& str)
{
	std::wstring returnVal;// = returnVal.assign(str.begin(), str.end());
	return returnVal = returnVal.assign(str.begin(), str.end());
}

Strings Split(const std::string& cs, char c)
{
	Strings r;
	std::string s(cs);
	while (true) 
	{
		int i = s.find(c);
		if (i == std::string::npos)
		{
			// No special character found, so push back the entire string and finish.
			r.push_back(s);
			break; 
		}    
		else
		{
			// Found the special character. 
			// Push back whatever is before the character, then work on the remainder
			// of the string.
			r.push_back(s.substr(0, i));
			s = s.substr(i + 1);
			Trim(&s);
			if (s.empty())
			{
				break;
			}
		}
	}
	return r;
}

void Trim(std::string* ps)
{
	assert(ps);
	while (!ps->empty() && (ps->at(0) == ' ' || ps->at(0) == '\r' || ps->at(0) == '\n' ) )
	{
		*ps = ps->substr(1);
	}
	while (!ps->empty() && (ps->at(ps->size() - 1) == ' ' || ps->at(ps->size() - 1) == '\r' || ps->at(ps->size() - 1) == '\n') )
	{
		*ps = ps->substr(0, ps->size() - 1);
	}
}